package cc.vicp.djx314.starfood.service.dishes.impls;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cc.vicp.djx314.starfood.dao.dish.DishMapper;
import cc.vicp.djx314.starfood.domain.dish.Dish;
import cc.vicp.djx314.starfood.service.dishes.DishByOneService;
import cc.vicp.djx314.starfood.service.exception.DishNotFoundException;

@Service
public class DishByOneServiceImpl implements DishByOneService {
	public static final Logger logger = LoggerFactory.getLogger(DishByOneServiceImpl.class);

	@Autowired
	DishMapper dishMapper;

	@Override
	@Transactional(readOnly = true)
	public Dish findDishByOne(Integer id) throws Exception {
		try {
			Dish dish = dishMapper.findDishByOne(id);
			if (dish == null) {
				throw new DishNotFoundException("id为" + id + "的菜式找不到");
			}
			return dish;
		} catch (Exception e) {
			logger.error("找不到菜式", e);
			throw e;
		}
	}

	@Override
	@Transactional(readOnly = true)
	public Resource getDishImage(Integer id) throws DishNotFoundException {
		Resource dishImage = dishMapper.getDishImg(id);
		if (dishImage == null) {
			throw new DishNotFoundException(new StringBuilder("id为：").append(id).append("的菜式找不到").toString());
		} else {
			return dishImage;
		}
	}
}